/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.Dimension;
import javax.swing.JLabel;

/**
 *
 * @author Grubi
 */
public class DiceField extends javax.swing.JPanel {
    private TableModel tm;
    static JLabel[] dices;
    static Dimension d = new Dimension(400, 400);
    /**
     * Creates new form JPanelDice
     */
    public DiceField() {
        initComponents();
        tm = new TableModel();
        jTable.setModel(tm);
        dices = new JLabel[5];
        dices[0] = jLabel1;
        dices[1] = jLabel2;
        dices[2] = jLabel3;
        dices[3] = jLabel4;
        dices[4] = jLabel5;
        for (int i = 0; i < 5; i++) {
            dices[i].setIcon(new javax.swing.ImageIcon(getClass().
                    getResource("/img/2d/" + (i+1) + ".png")));
            jPanelGameField.add(dices[i]);
            dices[i].setLocation(i, i);
        }
        this.setSize(d);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanelGameField = new javax.swing.JPanel();
        jPanelTable = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/white.png"))); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/white.png"))); // NOI18N

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/white.png"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/white.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/white.png"))); // NOI18N

        jPanelGameField.setBackground(new java.awt.Color(255, 255, 255));
        jPanelGameField.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanelGameField.setPreferredSize(new java.awt.Dimension(300, 300));

        javax.swing.GroupLayout jPanelGameFieldLayout = new javax.swing.GroupLayout(jPanelGameField);
        jPanelGameField.setLayout(jPanelGameFieldLayout);
        jPanelGameFieldLayout.setHorizontalGroup(
            jPanelGameFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 301, Short.MAX_VALUE)
        );
        jPanelGameFieldLayout.setVerticalGroup(
            jPanelGameFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 322, Short.MAX_VALUE)
        );

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable);

        javax.swing.GroupLayout jPanelTableLayout = new javax.swing.GroupLayout(jPanelTable);
        jPanelTable.setLayout(jPanelTableLayout);
        jPanelTableLayout.setHorizontalGroup(
            jPanelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
        );
        jPanelTableLayout.setVerticalGroup(
            jPanelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelGameField, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanelTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelGameField, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMouseClicked
        Result.getResult(getDices(), jTable.getSelectedRow()+1);
    }//GEN-LAST:event_jTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanelGameField;
    private javax.swing.JPanel jPanelTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    // End of variables declaration//GEN-END:variables

    private int[] getDices() {
        int[] a = new int[dices.length];
        for (int i = 0; i < dices.length; i++) {
            a[i] = getCount(dices[i]);
        }
        return a;
    }

    private int getCount(JLabel jLabel) {
        String s = jLabel.getIcon().toString();
        return Integer.getInteger(s.substring(s.length()-5, s.length()-4));
    }
    
    public void addUser(User u){
        tm.addUser(u);
    }

    public int getStatus() {
        return tm.getColumnCount();
    }

    public void rollDices() {
        for (int i = 0; i < dices.length; i++) {
            dices[i].setIcon(new javax.swing.ImageIcon(getClass().
                    getResource("/img/2d/" + 
                    ((int) (Math.random() * 100 % 6) + 1) + ".png")));
            System.out.println(Math.random()*1000  + " " + jPanelGameField.getWidth());
            dices[i].setLocation(
                    (int) (Math.random() * 10000 % jPanelGameField.getWidth()) -75, 
                    (int) (Math.random() * 10000 % jPanelGameField.getHeight()) -75);
        }
    }
}
